#!/usr/bin/python3
#
# gkmerge - merge Gatekeeper allowlist snippets
#
# gkmerge [--output name] files...
#	Takes GKE data from all files, merges it together, and writes it to a new snippet file 'output'.
#
from __future__ import print_function
import sys
import os
import signal
import errno
import subprocess
import argparse
import plistlib
import uuid


#
# Usage and fail
#
def fail(whatever):
	print("%s: %s" % (sys.argv[0], whatever), file=sys.stderr)
	sys.exit(1)


#
# Argument processing
#
parser = argparse.ArgumentParser()
parser.add_argument("--output", default="./snippet.gke", help="name of output file")
parser.add_argument('source', nargs='+', help='files generated by the gkrecord command')
args = parser.parse_args()


#
# Merge all the plist data from the input files, overriding with later files
#
gkedict = { }
for source in args.source:
	data = plistlib.readPlist(source)
	gkedict.update(data)


#
# Write it back out as a snippet file
#
plistlib.writePlist(gkedict, args.output)
print("Wrote %d authority records + %d signatures to %s" % (
	len(gkedict["authority"]), len(gkedict["signatures"]), args.output
))
